#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include "cJSON.h"
#include <string.h>
#define N 2048
char line[N]={'\0'};


//使用APIKEY和SECERTKEY获得token
char * get_token(const char * api_key,const char * secret_key)
{
	//open file
	//FILE * fp=open_file("1.txt");
	//打开一个内存文件
	FILE *fp=NULL;
	
	//参数1：指针的指针     响应消息的地址
	//参数2：文件大小的指针 响应消息的长度
	char * response;
	size_t resplen;
	//创建内存文件，当通过文件句柄写入数据时,会自动分配内存
	fp=open_memstream(&response,&resplen);
	if(NULL==fp)
	{
		perror("open_memstream error");
		return NULL;
	}
	
	
	
	//创建http客户端
	
	// 1初始化
	//CURL * curl=curl_easy_init();
	///
	CURL * curl=curl_easy_init();
	char * uri=NULL;
	asprintf(&uri,"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s",api_key,secret_key);//先分配一片内存,将数据放里面

	//2设置要访问的地址
	curl_easy_setopt(curl,CURLOPT_URL,uri);

	//3.将相应直接写入到该文件中
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
	//打印http请求和相应消息头
	//curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
	

	//3要访问的地址
	CURLcode error=curl_easy_perform(curl);
	if(error!=CURLE_OK)
	{
		perror("perform error\n");
		//fprintf(stderr,"");
		curl_easy_cleanup(curl);
		free(uri);
		free(response);
		fclose(fp);
		return NULL;
	}
	
	
	
	//4.清除curl
	curl_easy_cleanup(curl);
	free(uri);
	fclose(fp);
	//打印response
//	puts(response);
	
	//解析json字符串
	cJSON * json=cJSON_Parse(response);
	if(NULL==json)
	{
		const char * err_pos=cJSON_GetErrorPtr();
		if(NULL!=err_pos)
		{
			fprintf(stderr,"Error before:%s\n",err_pos);
		}
		return NULL;
	}
	
	
	
	cJSON * access_token= cJSON_GetObjectItemCaseSensitive(json, "access_token");
	

	if(!cJSON_IsString(access_token)){
	

		cJSON * err_des= cJSON_GetObjectItemCaseSensitive(json, "error_description");
		printf("%s",err_des->valuestring);
		return NULL;
	}
	

	//copy token str,free
	char * token=strdup(access_token->valuestring);

	//释放json数据结构占用的内存
	free(response);
	//
	cJSON_Delete(json);

	
	
	
	
	return token; 
	
	
	
}

//文本转语音
//text to audio
void text2speech(const char * token,const char * text)
{
	//1.初始化curl
	CURL * curl=curl_easy_init();
	
	//发送到百度云的字符串需要进行两次URL编码
	char * temp=curl_easy_escape(curl,text,strlen(text));
	printf("temp=%s\n",temp);
	
	
	char * data=curl_easy_escape(curl,temp,strlen(temp));
	printf("data=%s\n",data);
	
	curl_free(temp);
	
	char * postdata;
	//拼接POST请求放的数据
	asprintf(&postdata,"tex=%s&lan=zh&cuid=hqyj&ctp=1&aue=6&tok=%s",data,token);
	puts(postdata);
	
	
	//打开准备写入的音频文件
	FILE * fp=fopen("test.wav","w");
	if(NULL==fp)
	{
		perror("fopen failed");
		return ;
	}
	
	
	
	
	
	
	
	//2.设置
	curl_easy_setopt(curl,CURLOPT_URL,"https://tsn.baidu.com/text2audio");
	curl_easy_setopt(curl,CURLOPT_POST,1);
	
	curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postdata);
	
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
	
	CURLcode error=curl_easy_perform(curl);
	if(error!=CURLE_OK)
	{
		perror("perform error\n");
		//fprintf(stderr,"");
		curl_easy_cleanup(curl);
		fclose(fp);
		
	}
	
	
	
	//4.清除curl
	curl_easy_cleanup(curl);
	
	//关闭文件
	fclose(fp);
	system("aplay -q  test.wav");
}


int main(void)
{


//1.先获取到token 获取到一把钥匙
	char * token=get_token("x1pEj9Y46p4MibGolQ0wcE0w","G7I0VLbCozYjDiuGWEejqnEMKo9bsgV7");
	

	if(NULL==token)
	{
		return -1;
	}
	
	puts(token);
	
	while(1){
		
		fgets(line,sizeof(line),stdin);
		//printf("line =%s\n",line);
		if(strncmp(line,"quit",4)==0) break;
		text2speech(token,line);
	}
	return 0;
}
